package com.csl.ws.hotel.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;

import com.csl.ws.hotel.util.DateUtils;
import com.csl.ws.hotel.util.Encodes;

public class MainApp {

	public static void main(String[] args) throws Exception {
//		String imgFile="C:\\Users\\Administrator\\Pictures\\cs.jpg";
//		String strImg=getImageStr(imgFile);
//		System.out.println(strImg);
		
		String base64strString="/9j/4AAQSkZJRgABAgAAAQABAAD/4RB/RXhpZgAASUkqAAgAAAAKAA8BAgASAAAAhgAAABABAgAKAAAAmAAAABIBAwABAAAAAQAAABoBBQABAAAAogAAABsBBQABAAAAqgAAACgBAwABAAAAAgAAADEBAgAcAAAAsgAAADIBAgAUAAAAzgAAABMCAwABAAAAAQAAAGmHBAABAAAA4gAAAGIDAABOSUtPTiBDT1JQT1JBVElPTgBOSUtPTiBENTAALAEAAAEAAAAsAQAAAQAAAEFDRCBTeXN0ZW1zIERpZ2l0YWwgSW1hZ2luZwAyMDA5OjA5OjAzIDE4OjQ2OjUxACYAmoIFAAEAAACwAgAAnYIFAAEAAAC4AgAAIogDAAEAAAABAAAAAJAHAAQAAAAwMjEwA5ACABQAAADAAgAABJACABQAAADUAgAAAZEHAAQAAAABAgMAApEFAAEAAADoAgAABJIKAAEAAADwAgAABZIFAAEAAAD4AgAAB5IDAAEAAAAFAAAACJIDAAEAAAAAAAAACZIDAAEAAAAAAAAACpIFAAEAAAAAAwAAhpIHACwAAAAIAwAAkJICAAQAAAAxMDkAkZICAAMAAAAzMAAAkpICAAMAAAAzMAAAAKAHAAQAAAAwMTAwAaADAAEAAAABAAAAAqAEAAEAAACCAAAAA6AEAAEAAACgAAAABaAEAAEAAAA0AwAAF6IDAAEAAAACAAAAAKMHAAEAAAADAAAAAaMHAAEAAAABAAAAAqMHAAgAAABSAwAAAa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KqdQa9MvW1gp+2d56TToK6VVRgAYjwEm+qPL8V+nHW1rNFhlO/pk4I+0wbqbaLCl1bIw7MMGfR3UE5lbVaSjULyXVq6noGEuQzuPpnZNWMu10fPYjPS8Q+ljhrNA/Nj/bY/sZ5+/T2aewpdW1b+CMSzHJGXhRnhlD0izDMeDDBj2c6PoVGlp04CV1hR8ACc6liowOhOBiWW2aQuvO2/Y5mU3ZvJJHVFeyg7gSzMvVa96D6dKgsepO+JXN+vfBNpUfAAgFG5MfXVGniPqAe20DP3Eu6B7CjC5yzA5GfEm1FCXqA46HII7QatExdMzVWPl69dpI1Rqcq2/g+YTlVHe7OFUxWHEl2AkFgaxgi9TCiLI+HUetrzc24ryBnzLV3FBXslJYZI3OMyxRQKqSi7bbnuT5kF+lBwAO06JHJu2PT8VruflatkPnqJcdQyZHbcTMq0hS3IE1Kx7BJFOVHtJ7jeK6irUpyXVrYp7MMyTlwpiX8o+JPhD7M0/TnDSc+iR/5GH+G+G/8ACf8A2M1YRucv6J8cf4RON5Gg9zKZM43kLD3EiIdDkaNOcs3uJOZM1SkYA7TpT7QZ1ACsyFWyu2O4klV5Z+Rhg9j5kpAMjenLZXYwANTV6lWw3G4lHPeXlsIblf8AQyvqKvTfmA9rf0iyX2PB/RCdxiT6OocpsI69PtIApdgi7FjgnwJobVqFA7bCEUTN/QYxsYcoJhglp2BHOZyEAOcToDEcIECiA7QBjgAswzEV3hywJP/Z";
		/**
		boolean b=generateImage(base64strString);
		if (b) {
			System.out.println("生成图片成功！");
		}
		*/
		String  prefixpath="f:/etc/photo/123.jpg";
		boolean b=Encodes.generateImage(base64strString, prefixpath);
		System.out.println("生成图片："+b);
	//	createDirs(prefixpath);
	}
	
	public static void createDirs(String prefixpath) throws Exception{
	      Date date = new Date();   
//	      String path=prefixpath+new SimpleDateFormat("yyyy/MM/dd/").format(date);  
	      String path=prefixpath+DateUtils.getDate("yyyyMMdd");  
	      //如果不存在,创建文件夹  
	      File f = new File(path);  
	      if(!f.exists()){  
	          f.mkdirs();   
	      }  
	      System.out.println("filepath:"+f.getPath());
	      OutputStream out;  
	    //  UUID uuid = UUID.randomUUID();  
	      String pid="4101052039160530232612";
	      String fileName = pid + ".jpg";  
	      out = new FileOutputStream(f.getPath() +"\\"+ fileName);  
	      out.flush();  
	      out.close();  
	      String filePath=f.getPath()+File.separator+fileName;
	      System.out.println(filePath);
	}
	
	public static byte[] decode(final byte[] bytes) {  
        return Base64.decodeBase64(bytes);  
    }  
  
    /** 
     * 二进制数据编码为BASE64字符串 
     * 
     * @param bytes 
     * @return 
     * @throws Exception 
     */  
    public static String encode(final byte[] bytes) {  
        return new String(Base64.encodeBase64(bytes));  
    }  
    
    /**
     * 图片转换成Base64字符串
     * @param imgFile
     * @return
     */
	public static String getImageStr(String imgFile)
	{
		
		InputStream in=null;
		byte[] data=null;
		//
		try {
			in=new FileInputStream(imgFile);
			data=new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//对字节数组进行Base64编码,并返回Base64编码过的字节数组字符串
		return encode(data);
	}
	
	/**
	 * Base64字符串转换成图片
	 * @param imgStr
	 * @return
	 */
	public static boolean generateImage(String imgStr){
		//对字节数组字符串进行Base64解码并生成图片
		if (imgStr==null) { //图像数据为空
			return false;
		}
		try {
			//Base64编码
			byte[] b=decode(imgStr.getBytes());
			for (int i = 0; i < b.length; i++) {
				if (b[i]<0) { //调整异常数量
					b[i]+=256;
				}
			}
			//生成jpeg图片
			String imgFilePath="d:\\csl.jpg";
			OutputStream out=new FileOutputStream(imgFilePath);
			out.write(b);
			out.flush();
			out.close();
			
			return true;
		} catch (Exception e) {
			return false;
		}
	}

}
